<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">您需要先添加收货地址才可提交订单。</div>
      <template v-else>
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{ showAddress.receiver }}</li>
          <li><span>联系方式：</span>{{ showAddress.contact }}</li>
          <li><span>收货地址：</span>{{ showAddress.fullLocation + showAddress.address }}</li>
        </ul>
        <a href="javascript:;" @click="addressEditCom.open(showAddress)">修改地址</a>
      </template>
    </div>
    <div class="action">
      <XtxButton @click="openDialog()" class="btn">切换地址</XtxButton>
      <!-- 🔔 父组件通过 ref 绑定的名称，可调用子组件内部属性或方法 -->
      <XtxButton @click="addressEditCom.open({})" class="btn">添加地址</XtxButton>
    </div>
  </div>
  <!-- 💥 Vue3 新增的标签：瞬间移动/传送/瞬息移动/空间移动 到 body 后 -->
  <Teleport to="body">
    <XtxDialog
      title="收货地址管理"
      v-model="dialogVisible"
  >
    <div
      class="text item"
      v-for="item in addressList" :key="item.id"
      :class="{ active: selectedAddress && selectedAddress.id === item.id}"
      @click="selectedAddress = item"
    >
      <ul>
        <li><span>收<i/>货<i/>人：</span>{{ item.receiver }}</li>
        <li><span>联系方式：</span>{{ item.contact }}</li>
        <li><span>收货地址：</span>{{ item.fullLocation + item.address }}</li>
      </ul>
    </div>
    <template #footer>
      <XtxButton @click="dialogVisible = false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="confirmAddress()" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
  </Teleport>
  <!-- 🔔 添加和修改收货地址组件，ref 用于调用子组件内部属性或方法 -->
  <AddressEdit ref="addressEditCom" @on-success="getAddressList" />
</template>

<script>
import { ref, watch } from 'vue'
import AddressEdit from './address-edit.vue'
import { findAddressList } from '@/api/order'
export default {
  components: { AddressEdit },
  name: 'CheckoutAddress',
  // 💥 由于当前组件不止一个根元素，所以需要指定 emits，避免控制台会警告
  emits: ['change'],
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup (props, { emit }) {
    // 显示的地址：测试账号 sujiehao   密码：123456
    const showAddress = ref(null)
    if (props.list.length) {
      // 查找默认地址
      const defaultAddress = props.list.find(item => item.isDefault === 0)
      // 短路运算化简
      showAddress.value = defaultAddress || [...props.list][0]
    }

    // 注意：props 数据不能直接修改，所以需要声明一个新的变量，初始值为 props.list
    const addressList = ref(props.list)
    // 获取最新收货地址列表 - 地址添加或修改的时候触发这个函数
    const getAddressList = async (id) => {
      const { result } = await findAddressList()
      // 更新最新的地址列表
      addressList.value = result
      if (id) showAddress.value = addressList.value.find(item => item.id === id)
    }

    // 显示的地址有变化，就通知父组件
    watch(showAddress, () => {
      // 避免 null.id 报错，有 id 就通知父组件获取地址 id
      showAddress.value && emit('change', showAddress.value.id)
    }, { immediate: true })

    // 用于控制对话框显示隐藏双向绑定的
    const dialogVisible = ref(false)

    // 选择的地址(临时选中)
    const selectedAddress = ref(null)

    // 打开对话框
    const openDialog = () => {
      dialogVisible.value = true
      // 用于实现地址默认高亮
      selectedAddress.value = showAddress.value
    }

    // 确定收货地址
    const confirmAddress = () => {
      dialogVisible.value = false
      // 更新显示的地址
      showAddress.value = selectedAddress.value
    }

    // 🔔 添加和修改收货地址组件，用于调用子组件内部属性方法的
    const addressEditCom = ref(null)

    return { showAddress, dialogVisible, selectedAddress, openDialog, confirmAddress, addressEditCom, getAddressList, addressList }
  }
}
</script>

<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper){
    .body {
      height: 400px;
      overflow: auto;
    }
  }
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active, &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
